package com.qiaoba.module.log.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qiaoba.common.base.entity.param.TimeParam;
import com.qiaoba.common.base.exception.ServiceException;
import com.qiaoba.module.log.entity.SysLog;
import com.qiaoba.module.log.entity.param.SysLogParam;
import com.qiaoba.module.log.mapper.SysLogMapper;
import com.qiaoba.module.log.service.SysLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统日志 服务层实现
 *
 * @author ailanyin
 * @date 2023-09-23 22:12:49
 */
@Service
@RequiredArgsConstructor
public class SysLogServiceImpl implements SysLogService {

    private final SysLogMapper sysLogMapper;

    @Override
    public void insert(SysLog sysLog) {
        sysLogMapper.insert(sysLog);
    }

    @Override
    public List<SysLog> selectList(SysLogParam param) {
        return sysLogMapper.selectList(param2Wrapper(param));
    }

    @Override
    public void delLog(TimeParam param) {
        if (ObjectUtil.isEmpty(param.getBeginTime()) || ObjectUtil.isEmpty(param.getEndTime())) {
            throw new ServiceException("开始时间-结束时间必填！");
        }
        QueryWrapper<SysLog> wrapper = new QueryWrapper<>();
        wrapper.lambda().between(SysLog::getOperationTime, param.getBeginTime(), param.getEndTime());
        sysLogMapper.delete(wrapper);
    }

    @Override
    public int delete(List<String> ids) {
        return sysLogMapper.deleteBatchIds(ids);
    }

    private QueryWrapper<SysLog> param2Wrapper(SysLogParam param) {
        QueryWrapper<SysLog> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .orderByDesc(SysLog::getOperationTime)
                .between(ObjectUtil.isNotEmpty(param.getBeginTime()) && ObjectUtil.isNotEmpty(param.getEndTime()),
                        SysLog::getOperationTime, param.getBeginTime(), param.getEndTime())
                .like(StrUtil.isNotBlank(param.getTitle()), SysLog::getTitle, param.getTitle())
                .like(StrUtil.isNotBlank(param.getOperatorName()), SysLog::getOperatorName, param.getOperatorName())
                .eq(ObjectUtil.isNotEmpty(param.getBusinessType()), SysLog::getBusinessType, param.getBusinessType())
                .eq(ObjectUtil.isNotEmpty(param.getStatus()), SysLog::getStatus, param.getStatus());
        return wrapper;
    }
}
